﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL = KC.Business.BlogSlinger;
using DAL = KC.DataAccess.BlogSlinger;
using txt = KC.Business.Text;

public partial class search : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		if (Request.QueryString["Query"] == null)
			return;
		string query = Request.QueryString["Query"];
		if (string.IsNullOrEmpty(query))
			return;

		BLL.LogActivity(ref Master.dc, Master.user, Request.UserHostAddress, "Ran Search Engine", "query: " + query, Request.Url.AbsoluteUri, Request.UrlReferrer.AbsoluteUri);

		lblQuery.Text = Server.HtmlEncode(query);

		rUsers.DataSource = BLL.SearchUsers(ref Master.dc, query);
		rUsers.DataBind();
		rBlogs.DataSource = BLL.SearchBlogs(ref Master.dc, query);
		rBlogs.DataBind();
		rPosts.DataSource = BLL.SearchPosts(ref Master.dc, query);
		rPosts.DataBind();
    }

	protected void rUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
			return;
		DAL.User user = (DAL.User)e.Item.DataItem;

		HyperLink hlFacebook = (HyperLink)e.Item.FindControl("hlFacebook");
		Image image = (Image)e.Item.FindControl("iPost");
		Label lblUsername = (Label)e.Item.FindControl("lblUsername");

		hlFacebook.NavigateUrl = BLL.GetUserFacebookURL(ref Master.dc, user.ID);
		image.ImageUrl = BLL.GetUserImageURL(ref Master.dc, user);
		lblUsername.Text = Server.HtmlEncode(txt.StripHTML(user.DisplayName));
	}

	protected void rBlogs_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
			return;
		DAL.Blog blog = (DAL.Blog)e.Item.DataItem;
		DAL.User user = BLL.GetUserByBlog(ref Master.dc, blog);

		HyperLink hlBlog = (HyperLink)e.Item.FindControl("hlBlog");
		HyperLink hlFacebook = (HyperLink)e.Item.FindControl("hlFacebook");
		Label lblBlog = (Label)e.Item.FindControl("lblBlog");
		Label lblUsername = (Label)e.Item.FindControl("lblUsername");
		Image iPost = (Image)e.Item.FindControl("iPost");

		hlBlog.Text = Server.HtmlEncode(txt.StripHTML(blog.Name));
		hlBlog.NavigateUrl = "Blog.aspx?ID=" + blog.ID.ToString();
		lblBlog.Text = string.IsNullOrEmpty(blog.Description)
			? ""
			: ": " + Server.HtmlEncode(txt.StripHTML(blog.Description));
		hlFacebook.NavigateUrl = BLL.GetUserFacebookURL(ref Master.dc, user.ID);
		iPost.ImageUrl = BLL.GetUserImageURL(ref Master.dc, user);
		lblUsername.Text = user.DisplayName;
	}

	protected void rPosts_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
			return;
		DAL.Post post = (DAL.Post)e.Item.DataItem;
		DAL.User user = BLL.GetUserByPost(ref Master.dc, post);


		HyperLink hlFacebook = (HyperLink)e.Item.FindControl("hlFacebook");
		HyperLink hlBlog = (HyperLink)e.Item.FindControl("hlBlog");
		HyperLink hlPost = (HyperLink)e.Item.FindControl("hlPost");
		Image iPost = (Image)e.Item.FindControl("iPost");
		Label lblUsername = (Label)e.Item.FindControl("lblUsername");
		Label lblPost = (Label)e.Item.FindControl("lblPost");

		hlFacebook.NavigateUrl = BLL.GetUserFacebookURL(ref Master.dc, user.ID);
		hlBlog.NavigateUrl = "Blog.aspx?ID=" + post.BlogID.ToString();
		hlBlog.Text = Server.HtmlEncode(txt.StripHTML(post.Blog.Name));
		hlPost.Text = Server.HtmlEncode(txt.StripHTML(post.Title));
		hlPost.NavigateUrl = "Post.aspx?Title=" + post.FriendlyName;
		iPost.ImageUrl = BLL.GetUserImageURL(ref Master.dc, post);
		lblUsername.Text = Server.HtmlEncode(txt.StripHTML(user.DisplayName));
		lblPost.Text = post.Excerpt;
		if (string.IsNullOrEmpty(lblPost.Text.Trim()))
			lblPost.Text = post.Body;
		if (lblPost.Text.Length > 512)
			lblPost.Text = lblPost.Text.Substring(0, 512);
		lblPost.Text = Server.HtmlEncode(txt.StripHTML(lblPost.Text));
	}

}